#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
#define NUM 5

void* threadRun(void* name)
{
   // while(true)
    //{
        cout << "我是次线程，我正在运行..." << (char*)name <<  endl;
        sleep(1);
    //}
    delete[] (char*)name;

    pthread_exit((void*)"EXIT");
    return nullptr;
}

int main()
{
    pthread_t pt[NUM];
    for(int i = 0; i < NUM; i++)
    {
        char *name = new char[64];
        snprintf(name, 64, "thread-%d", i+1);
        pthread_create(pt + i, nullptr, threadRun, name);
    }
    //pthread_create(&t, nullptr, threadRun, nullptr);
    //等待次线程运行结束
    
    void *retval = nullptr;
    for(int i = 0; i < NUM; i++)
    {
        int ret = pthread_join(pt[i], &retval);
        if(ret != 0)
            cerr << "等待线程" << pt[i] << "失败" << endl; 
        cout << "线程 " << pt[i] << " 等待成功，退出信息是 " << (const char*)retval << endl;
    }
    cout << "所有线程都推出了" << endl;

    //while(true)
    //{
    //    cout << "我是主线程" << endl;
    //    sleep(1);
    //}
    return 0;
}
